resource "alicloud_db_readonly_instance" "main" {
  count                 = var.number_of_instances 
  master_db_instance_id = var.rds_master_db
  engine_version        = var.rds_engine_version
  instance_type         = var.rds_instance_type
  instance_storage      = var.rds_instance_storage
  instance_name         = "${var.rds_instance_name}-readonly"
  vswitch_id            = var.rds_vswitch_id[count.index]
  dynamic "parameters" {
    for_each            = var.rds_parameters
    content {
      name              = lookup(parameters.value, "name", null)
      value             = parameters.value.value
    }
  }  
}

resource "alicloud_db_read_write_splitting_connection" "main" {
  count             = var.enable_rds_rws ? 1 : 0
  instance_id       = var.rds_master_db
  connection_prefix = var.rds_rws_connection_prefix != "" ? var.rds_rws_connection_prefix : var.rds_instance_name
  distribution_type = var.rds_rws_distribution_type
  port              = var.rds_port
  max_delay_time    = var.rds_rws_max_delay_time
  weight            = var.rds_rws_weight
  depends_on        = [alicloud_db_readonly_instance.main]
}